package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.JiatiaoDao;
import com.dao.UserDao;
import com.po.Jiatiao;
import com.po.User;

@Repository("userService")
public class UserService {

	private UserDao ud;
	private JiatiaoDao jtd;
	public User findByUname(String uname, String pwd) {
		
		User user= new User();
		user.setUname(uname);
		user.setUpwd(pwd);
		User u1 = ud.findForObject(user);
		User u = ud.getByName(uname);
		if(u != null){
			if(u.getUpwd().equals(pwd))
				return u;
			else
				return null;
		}
		// TODO Auto-generated method stub
		return null;
	}

	public boolean addUser(String uname, String pwd) {
		// TODO Auto-generated method stub
		User user= new User();
		user.setUname(uname);
		user.setUpwd(pwd);
		try{
			ud.add(user);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public boolean addJiatiao(int uid, String content) {
		// TODO Auto-generated method stub
		Jiatiao jt = new Jiatiao();
		jt.setContent(content);
		jt.setGrade(1);
		try{
			jt.setUser(ud.findById(uid));
			jtd.add(jt);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public boolean setOk(int jid) {
		// TODO Auto-generated method stub
		
		try{
			Jiatiao jt = jtd.findById(jid);
			jt.setGrade(jt.getGrade()+2);
			jtd.update(jt);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public boolean setBack(int jid) {
		// TODO Auto-generated method stub
		
		try{
			Jiatiao jt = jtd.findById(jid);
			jt.setGrade(jt.getGrade()-1);
			jtd.update(jt);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public List<Jiatiao> getJiatiaoAll() {
		try {
			return jtd.findAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public List<Jiatiao> getWorksById(User u) {
		return jtd.getListByGrade(u.getGrade()-2);
	}

	public User getById(Integer id) {
		// TODO Auto-generated method stub
		try {
			return ud.findById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
